<?php

/**
 * SigmaCMS - Content Management System
 *
 * Copyright (C) 2008 Pavol Biely
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    SigmaCMS
 * @author     Pavol Biely <pavolbiely@gmail.com>
 * @copyright  2008 Pavol Biely
 * @license    http://www.gnu.org/licenses/gpl.txt   GNU General Public License
 * @link       http://pabi3.com/
 * @since      0.1.0
 */

final class Admin_Languages
{
	/**
	 * Initialization
	 *
	 * @return void
	 */
	public function initialize()
	{
		Admin::header(_ADMIN_LANGUAGES);

		echo "<h1>"._ADMIN_LANGUAGES."</h1>\n";

		// path to languages
		$path = dirname(__FILE__).'/../../'.cfg::DIR_LANGS;

		// available languages
		$langs = (array) glob($path.'*.php');

		// languages names by ISO 639-1
		$i18n = i18n::getLanguages();

		// installed languages
		$installed = array();

		// load installed languages
		$sql = db::query("SELECT `lang` FROM ".db::prefix('languages'));
		while ($row = db::fetchObject($sql)) {
			$installed[] = $row->lang;
		}
		db::free($sql);

		echo "<table>\n";
		echo "<colgroup>\n";
		echo "<col width='1%' align='center' />\n";
		echo "<col width='39%' align='left' />\n";
		echo "<col width='20%' align='center' />\n";
		echo "<col width='20%' align='center' />\n";
		echo "<col width='20%' align='center' />\n";
		echo "</colgroup>\n";

		echo "<tr>\n";
		echo "<th colspan='2'>"._ADMIN_LANGUAGES_ONE."</th>\n";
		echo "<th>"._ADMIN_LANGUAGES_ACTIVE."</th>\n";
		echo "<th>"._ADMIN_LANGUAGES_INSTALL_STATUS."</th>\n";
		echo "<th>"._ADMIN_ACTIONS."</th>\n";
		echo "</tr>\n";

		// print installed languages
		foreach ($langs as $key => $value) {
			// get language abbreviation
			preg_match('/\/([a-z]{2})\.php$/',$value,$matches);

			// abbreviation of language by ISO 639-1
			$lang = $matches[1];

			echo "<tr>\n";
			echo "<td>\n";
			echo "<img src='../".cfg::DIR_LANGS.$lang.".png' alt='$lang' width='16' height='11' />\n";
			echo "</td>\n";

			echo "<td>\n";
			if (array_key_exists($lang,$i18n)) {
				echo $i18n[$lang]."\n";
			} else {
				echo "$lang\n";
			}
			echo "</td>\n";

			echo "<td>\n";
			if ($lang == cfg::get('language')) {
				echo Admin::image('true',_ADMIN_YES);
			} else {
				echo Admin::image('false',_ADMIN_NO);
			}
			echo "</td>\n";

			echo "<td>\n";
			if (in_array($lang,$installed)) {
				echo Admin::image('true',_ADMIN_YES);
			} else {
				echo Admin::image('false',_ADMIN_NO);
			}
			echo "</td>\n";

			echo "<td>\n";
			if ($lang == cfg::get('language')) {
				echo "-\n";
			} else {
				if (in_array($lang, $installed)) {
					echo "<a href='index.php?action=languages&amp;page=uninstall&amp;language=$lang'>"._ADMIN_LANGUAGES_UNINSTALL."</a>\n";
				} else {
					echo "<a href='index.php?action=languages&amp;page=install&amp;language=$lang'>"._ADMIN_LANGUAGES_INSTALL."</a>\n";
				}
			}
			echo "</td>\n";
			echo "</tr>\n";
		}

		echo "</table>\n";

		Admin::footer();
	}

	/**
	 * Install language
	 *
	 * @return void
	 */
	public function page_install()
	{
		// abbreviation of language must be passed in URL
		if (isset($_GET['language'])) {
			$lang = substr(_get('language'), 0, 2);
		} else {
			Admin::redirect('index.php?action=languages');
		}

		// is language already installed?
		$installed = db::fQuery("SELECT `id` FROM ".db::prefix('languages')." WHERE `lang`='".db::escape($lang)."'");
		if ($installed === false) {
			// path to language
			$path = dirname(__FILE__).'/../../'.cfg::DIR_LANGS;

			// does the language file and it's flag/image exist?
			if (file_exists($path.$lang.'.php') && file_exists($path.$lang.'.png')) {
				// insert langauge to database
				$sql = db::query("INSERT INTO ".db::prefix('languages')."(`lang`) VALUES('".db::escape($lang)."')");

				// the last language ID
				$id = db::lastId();

				// success?
				if ($sql) {
					$langs = i18n::getLanguages();
					Actionlog::add(sprintf(_LOG_ADMIN_LANGUAGE_ADD, $langs[$lang]));

					// event after language is saved
					Admin::addEvent('languageAdd', $id, $lang);
				}
			}
		}

		Admin::redirect('index.php?action=languages');
	}

	/**
	 * Uninstall language
	 *
	 * @return void
	 */
	public function page_uninstall()
	{
		// abbreviation of language must be passed in URL
		if (isset($_GET['language'])) {
			$lang = substr(_get('language'), 0, 2);
		} else {
			Admin::redirect('index.php?action=languages');
		}

		// language, which user is uninstalling cannot be active
		if ($lang == cfg::get('language')) {
			Admin::redirect('index.php?action=languages');
		}

		// get language ID
		$id = i18n::getLangId($lang);

		// delete language from database
		db::query("DELETE FROM ".db::prefix('languages')." WHERE `lang`='".db::escape($lang)."'");

		$langs = i18n::getLanguages();
		Actionlog::add(sprintf(_LOG_ADMIN_LANGUAGE_DELETE, $langs[$lang]));

		// event after language deletion
		Admin::addEvent('languageDelete', $id, $lang);

		// redirect
		Admin::redirect('index.php?action=languages');
	}
}
